package com.example.xm.service.impl;

import com.example.xm.entity.PinglunEntity;
import com.example.xm.mapper.PinglunMapper;
import com.example.xm.service.IPinglunService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.image.ImageProducer;
import java.util.ArrayList;
import java.util.List;
@Service
public class PinglunService implements IPinglunService {
    @Autowired
    PinglunMapper pinglunMapper;
    @Override
    public PinglunEntity add(PinglunEntity pinglunEntity) {
        pinglunMapper.add(pinglunEntity);
        PinglunEntity entity=  pinglunMapper.getById(pinglunEntity.getId());
        return entity;
    }

    @Override
    public List<PinglunEntity> list(Integer productId) {
        List<PinglunEntity> list=pinglunMapper.list(productId);
        List<PinglunEntity> yiList=new ArrayList<>();
        for (PinglunEntity entity : list) {
            if(entity.getPid()==0){
                yiList.add(entity);
            }
        }
        for (PinglunEntity entity : yiList) {
            for (PinglunEntity pinglunEntity : list) {
                if(entity.getId()==pinglunEntity.getPid()){
                    entity.getChildren().add(pinglunEntity);
                }
            }
        }
        return yiList;
    }
}
